/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：定义UI窗体类的基类
* CreateTime：2020-04-05 20:19:31
* Version：1.0
* Modify Recorder：
*************************************************/

using UnityEngine;

namespace TinyToolKit.UI
{
    [DisallowMultipleComponent]
    public class UIFormBase : MonoBehaviour
    {
        public UIFormLayerType UIFormLayerType = UIFormLayerType.Normal;
        public UIFormShowType UIFormShowType = UIFormShowType.Simple;
        public UIFormRaycastType UIFormRaycastType = UIFormRaycastType.Allow;
        public bool isClearOrder;

        private TUIManager _uiManager;
        private string _uiFormName;
        private UIFormStateType UIFormStateType = UIFormStateType.None;

        protected TUIManager UIManager => _uiManager;
        public string GetUIFormName => _uiFormName;
        public UIFormStateType GetStateType => UIFormStateType;

        #region Life Cycle
        /// <summary> 
        /// 初始化
        /// </summary>
        protected internal virtual void OnInit()
        {
        }
        
        /// <summary> 
        /// 界面进入
        /// </summary>
        protected internal virtual void OnEnter()
        {
            UIFormStateType = UIFormStateType.Enter;
        }

        /// <summary>
        /// 界面暂停(弹出了其他界面)
        /// </summary>
        protected internal virtual void OnPause()
        {
            UIFormStateType = UIFormStateType.Pause;
        }

        /// <summary>
        /// 界面继续(其他界面移除，回复本来的界面交互)
        /// </summary>
        protected internal virtual void OnResume()
        {
            UIFormStateType = UIFormStateType.Resume;
        }

        /// <summary>
        /// 界面退出,退出这个界面，界面被关闭
        /// </summary>
        protected internal virtual void OnExit()
        {
            UIFormStateType = UIFormStateType.Exit;
        }
        #endregion

        #region Helper Func
        public void SetUIManager(string name,TUIManager uiManager)
        {
            _uiFormName = name;
            _uiManager = uiManager;
        }
        
        public UIFormBase OpenUIForm(string uiFormName)
        {
            return _uiManager.OpenUIForm(uiFormName);
        }

        public void CloseUIForm(string uiFormName) 
        {
            _uiManager.CloseUIForm(uiFormName); 
        }
         
        public void Close()
        {
            _uiManager.CloseUIForm(_uiFormName); 
        }

        [ContextMenu("Unify Name")]
        public void UnifyName() 
        {
            gameObject.name = GetType().ToString();
        }
        #endregion
    }
}
